﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Krile.Forms.MainForm.Controls
{
    public partial class SearchBar : UserControl
    {
        public SearchBar()
        {
            this.InitializeComponent();
        }

        public event EventHandler OnSearchActivatedChanged;

        public event EventHandler OnSearchNeedleChanged;

        public event EventHandler OnCloseRequired;

        public event EventHandler OnNewTabCreateRequired;

        public event EventHandler OnChangeVPosRequired;

        bool _searchactivated = false;
        public bool SearchActivated
        {
            get { return _searchactivated; }
            private set
            {
                _searchactivated = value;
                if (OnSearchActivatedChanged != null)
                    OnSearchActivatedChanged.Invoke(this, new EventArgs());
            }
        }

        bool _cse = false;
        public bool CustomSearchEnabled
        {
            get { return _cse; }
            set
            {
                _cse = value;
                MotherPanel.BackColor = value ? SystemColors.Control : Color.White;
                MotherPanel.Enabled = !value;
                needleInput.Enabled = !value;
                if (value)
                    needleInput.Text = "[custom search enabled]";
                else
                    needleInput.Text = String.Empty;
                AddTab.Enabled = !value;
            }
        }

        public enum SearchTypes { All, Text, User }

        SearchTypes _currentsearchtype = SearchTypes.All;
        public SearchTypes CurrentSearchType {
            get { return _currentsearchtype; }
            set
            {
                _currentsearchtype = value;
                if (OnSearchNeedleChanged != null)
                    OnSearchNeedleChanged.Invoke(this, new EventArgs());
            }
        }

        string _searchneedle = String.Empty;
        public string SearchNeedle
        {
            get { return _searchneedle; }
            set
            {
                _searchneedle = value;
                if (needleInput.Text != value)
                {
                    needleInput.Text = value;
                }
                else
                {
                    AddTab.Enabled = !String.IsNullOrEmpty(_searchneedle);
                    if (String.IsNullOrEmpty(_searchneedle) == SearchActivated)
                        SearchActivated = !String.IsNullOrEmpty(_searchneedle);
                    if (OnSearchNeedleChanged != null)
                        OnSearchNeedleChanged.Invoke(this, new EventArgs());
                }
            }
        }

        public void Clear()
        {
            SearchNeedle = String.Empty;
        }

        private void searchMode_Click(object sender, EventArgs e)
        {
            searchFromAll.Checked = CurrentSearchType == SearchTypes.All;
            searchFromText.Checked = CurrentSearchType == SearchTypes.Text;
            searchFromUser.Checked = CurrentSearchType == SearchTypes.User;
            searchModeContext.Show(searchMode, new Point(0, searchMode.Height));
        }

        private void MainInput_TextChanged(object sender, EventArgs e)
        {
            SearchNeedle = needleInput.Text;
        }

        private void AddTab_Click(object sender, EventArgs e)
        {
            if (OnNewTabCreateRequired != null)
                OnNewTabCreateRequired.Invoke(this, new EventArgs());
        }

        private void Closer_Click(object sender, EventArgs e)
        {
            Clear();
            if (OnCloseRequired != null)
                OnCloseRequired.Invoke(this, new EventArgs());
        }

        private void changePlacement_Click(object sender, EventArgs e)
        {
            if (OnChangeVPosRequired != null)
                OnChangeVPosRequired.Invoke(this, new EventArgs());
        }

        public void SetFocus()
        {
            needleInput.Focus();
        }

        private void searchFromAll_Click(object sender, EventArgs e)
        {
            CurrentSearchType = SearchTypes.All;
            searchMode.Image = Properties.Resources.search;
        }

        private void searchFromText_Click(object sender, EventArgs e)
        {
            CurrentSearchType = SearchTypes.Text;
            searchMode.Image = Properties.Resources.searchtext;
        }

        private void searchFromUser_Click(object sender, EventArgs e)
        {
            CurrentSearchType = SearchTypes.User;
            searchMode.Image = Properties.Resources.searchuser;
        }

        private void MainInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                Closer_Click(sender, e);
            }
        }
    }
}
